import { axios } from "@/services/axios";
import {useEffect, useMemo, useState} from "react";

const QueryShowField = {
  get: data => axios.get(`api/queryshowfield/${data.id}`),
  getlist: params => axios.post(`api/queryshowfield/list`, params),
  create: data => axios.post("api/queryshowfield", data),
  getid: data => axios.get("api/queryshowfield", data),
  save: data => axios.post(`api/queryshowfield/${data.id}`, data),
  delete: data => axios.post(`api/queryshowfield/delete/${data.id}`),
  getbycode: code => axios.get(`api/queryshowfieldcode/${code}`),
};

export function useQueryShowfieldItem(typename) {
  const [queryshowfielddataLoaded, setqueryshowfielddataLoaded] = useState(false);
  const [queryshowfielddata, setqueryshowfielddata] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    QueryShowField.getbycode(typename).then(data => {
      if (!cancelDataSourceLoading) {
        setqueryshowfielddataLoaded(true);
        setqueryshowfielddata(data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [typename]);

  return useMemo(() => ({ queryshowfielddataLoaded, queryshowfielddata }), [queryshowfielddataLoaded, queryshowfielddata]);
}
export default QueryShowField;